﻿using BZ.Logger;
using HalconDotNet;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.Monitor;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;

namespace NDK.Module.StationLogic
{
    public class Station_转盘四穴 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_转盘四穴 Instance = new Station_转盘四穴();
        public Stopwatch SunWay04 = new Stopwatch();
        public string StationName = "Station04转盘4工位";

        //步骤字段、属性
        private StationStep _Step = StationStep.判断是否工作;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if (_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        public HImage image1;
        public HImage image2;
        public HImage image3;
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            StationID = 4;
            CurHoleId = 6;
            logBaseInfo = "转盘四穴流程";
            Step = StationStep.判断是否工作;
            MVCCD = Common.HikVisionList[0];
            QueueImage = new ConcurrentQueue<MeasureImage>();
        }

        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.判断是否工作:
                        if (!Common.AllStationCanWork)
                            break;
                        if (!Common.DicStationWorkStatus[StationID])
                        {
                            Step = Common.DicHoleCanWork[CurHoleId] ? StationStep.触发相机拍照1 : StationStep.工位流程完成;
                            SunWay04.Restart();
                        }
                        break;

                    case StationStep.触发相机拍照1:
                        if (Common.CurProType != "Product" || Common.CurProType != "Product1")
                        {
                            Machine.imageAllLight[CurHoleId - 1].Open();
                        }
                        Machine.imageLights_四工站蓝光光源.OpenAll();
                        Thread.Sleep(Common.IP_光源延时);
                        if (Common.IsEmptyModel)
                        {
                            Thread.Sleep(1000);
                            Step = StationStep.工位流程完成;
                        }
                        else if (CCDImage(ref HImageCCD))
                        {
                            image1 = HImageCCD.Clone();
                            if (Common.CurProType.Contains("Product"))
                            {
                                Step = StationStep.触发相机拍照2;
                            }
                            else
                            {
                                QueueImage.Enqueue(new MeasureImage() { Hole = CurHoleId, Images = new (string, HImage)[] { ("斜CCD1", image1) } });

                                if (Common.CurProType != "Product" || Common.CurProType != "Product1")
                                {
                                    Machine.imageAllLight[CurHoleId - 1].Close();
                                }
                                Machine.imageLights_四工站蓝光光源.CloseAll();
                                Step = StationStep.工位流程完成;
                            }
                        }
                        break;

                    case StationStep.触发相机拍照2:
                        Machine.imageLights_四工站蓝光光源2.OpenAll();
                        Thread.Sleep(Common.IP_光源延时);
                        if (CCDImage(ref HImageCCD))
                        {
                            image2 = HImageCCD.Clone();
                            Step = StationStep.触发相机拍照3;
                        }
                        //Machine.imageLights_四工站蓝光光源2.Close();
                        break;

                    case StationStep.触发相机拍照3:
                        Machine.imageLights_四工站蓝光光源3.OpenAll();
                        Thread.Sleep(Common.IP_光源延时);
                        if (CCDImage(ref HImageCCD))
                        {
                            image3 = HImageCCD.Clone();
                            QueueImage.Enqueue(new MeasureImage() { Hole = CurHoleId, Images = new (string, HImage)[3] { ("斜CCD1", image1), ("斜CCD2", image2), ("斜CCD3", image3) } });
                        }
                        Machine.imageLights_四工站蓝光光源3.CloseAll();   //TOM修改，新增图片光源组
                        Machine.imageAllLight[CurHoleId - 1].Close();
                        Step = StationStep.工位流程完成;
                        break;

                    case StationStep.工位流程完成:
                        CurHoleId++;
                        if (CurHoleId > 8)
                        {
                            CurHoleId = 1;
                        }
                        Common.ChangeTurnTableStatus(StationID, true);
                        Step = StationStep.判断是否工作;
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess()
        {
            ActionProcess();
        }

        public override void GRRActionProcess()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            判断是否工作,
            触发相机拍照1,
            触发相机拍照2,
            触发相机拍照3,
            工位流程完成
        }
    }

}
